package com.java.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.Serializable;

/**
 * @Author: ZhangJin
 * @Date: 2020/9/9 22:19
 */
public class TestRedisPool implements Serializable {

    private static JedisPool pool = null;

    /**
     * 获取RedisPool实例（单例）
     * @return RedisPool实例
     */
    public static JedisPool getJedisPoolInstance(String ip,int port,String password) {
        synchronized (TestRedisPool.class) {
            if (pool == null) {
                JedisPoolConfig config = new JedisPoolConfig();
                config.setMaxTotal(500);
                config.setMaxIdle(30);
                config.setMinIdle(5);
                config.setMaxWaitMillis(1000 * 10);
                config.setTestWhileIdle(false);
                config.setTestOnBorrow(false);
                config.setTestOnReturn(false);
                pool = new JedisPool(config, ip, port ,100000,password);
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return pool;
        }
    }


    public TestRedisPool(String ip, int port) {

    }

    /**
     * 将Jedis对象（连接）归还连接池
     * @param jedis 连接对象
     */
    public static void returnResource(Jedis jedis) {
        if(jedis != null) {
            jedis.close();
        }
    }
}